package controllers;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import play.mvc.Controller;


import models.Barco;
import models.Categoria;
import models.Regata;
import models.Pontuacao;

public class visualizarRegatas extends Application {

	private static Connection conexao;
	private static Statement comando;
	
	/*************************************************************/
	
	protected static void conectar() throws SQLException {  
	    
		  
		try {
			Class.forName("org.postgresql.Driver");
		} catch (ClassNotFoundException e) {
		}

			conexao = DriverManager.getConnection(  
			        "jdbc:postgresql://localhost:5432/SGRv2",  
			        "postgres",  
			        "senha");
		
			comando =  conexao.createStatement();

	}

	/***********************************************************/
	
	public static void visualizarRegatas(int idrodada) throws SQLException {
		

		String rodadaid = "";
		try
		{		
			rodadaid  =  request.querystring.toString();

			rodadaid  = rodadaid .substring(5);
			idrodada = Integer.parseInt(rodadaid );
			
		}
		catch (Exception e)
		{
			
		}
		if(idrodada == 0)
		{
			visualizarCompeticoes.visualizarCompeticoes();			
		}
		else
		{
			
			//select * from relacaobarcocompeticao where idcompeticao = (select idcompeticao from rodada where id = (select idrodada from regata where id = 66))
			
			//List<Barco> barcosregata1 = new ArrayList<Barco>();
			List<Barco> barcosparticipantes = new ArrayList<Barco>();
			
			//barcosregata1 = Barco.recuperarBarcos("select * from barco INNER join  (select * from relacaobarcocompeticao where idcompeticao = (select idcompeticao from rodada where id = (select idrodada from regata where id = 387))) as T1 on barco.idbarco = T1.idbarco ");
			barcosparticipantes = Barco.recuperarBarcos("select * from barco INNER join  (select * from relacaobarcocompeticao where idcompeticao = (select idcompeticao from rodada where id = " + idrodada + " )) as T1 on barco.idbarco = T1.idbarco");
			
			List<Regata> regatas = new ArrayList<Regata>();
			regatas  = Regata.Regatas(idrodada);
			
			List<Pontuacao> pontosregata1  = new ArrayList<Pontuacao>();
			List<Pontuacao> pontosregata2  = new ArrayList<Pontuacao>();
			
			
		     //regata 1 
			for(Barco barc : barcosparticipantes) {
			
				Pontuacao novo = new Pontuacao(); 
				novo = Pontuacao.Pontos(barc.getid(), regatas.get(0).getid() );
				novo.setidregata(regatas.get(0).getid());
				novo.setidbarco(barc.getid());
				novo.setnomebarco(barc.getnome());
				pontosregata1.add(novo);
			}
		  
			//regata 2
			for(Barco barc : barcosparticipantes) {
				
				Pontuacao novo = new Pontuacao(); 
				novo = Pontuacao.Pontos(barc.getid(), regatas.get(1).getid() );
				novo.setidregata(regatas.get(1).getid());
				novo.setidbarco(barc.getid());
				novo.setnomebarco(barc.getnome());
				pontosregata2.add(novo);
			}
			
			
		
			render(pontosregata1, pontosregata2 );
		}
	}	
	
    /***************************************************************/
}






















